﻿(function ()
{
	nWse.stPageInit.cAddEvtHdlr_DocRdy(function()
	{
		var nMvc = nWse.nMvc;
		var nGui = nWse.nGui;

		var stAppCnsl = nWse.stAppCnsl;

		//-------- GUI框架初始化

		// 装载模型
		var l_RootMdl = nMvc.tMdl.sc_Root = new nMvc.tMdl();
		l_RootMdl.cCrtFld("CrntTab", "");
		fInitMdl();

		// 初始化命令
		fInitCmds();

		// 初始化框架，延迟运行
		nGui.stFrmwk.cInit({
			c_RootPutSrc: document.getElementById("k_RootPutSrc"),
			c_Hbps: [320, 1200],
			c_FadeIn: true
		});

		setTimeout(function ()
		{
			nGui.stFrmwk.cRun();
		}, 300);

		//-------- 模型

		function fInitMdl()
		{
			var l_MdlData = {
				"username": {
					"Wse_Val": "",
					"Wse_Optns": {
						"c_Rqrd": 1
					},
					"Wse_Vldtrs": [
					]
				}
				,
				"password": {
					"Wse_Val": "",
					"Wse_Optns": {
						"c_Rqrd": 1
					},
					"Wse_Vldtrs": [
						{
							"c_Tpnm": "tStrLenRge",
							"c_Info": "密码长度 8 - 20 个字符",
							"c_Min": 8,
							"c_Max": 20
						}
					]
				}
			};
			var l_Mdl = new nMvc.tMdl(l_MdlData, true);
			l_Mdl.cAllFldsAcpErr(true);
			l_RootMdl.cCrtFld("login", l_Mdl);

			l_MdlData = {
				"username": {
					"Wse_Val": "",
					"Wse_Optns": {
						"c_Rqrd": 1
					},
					"Wse_Vldtrs": [
						{
							"c_Tpnm": "tStrLenRge",
							"c_Info": "用户名长度 2 - 20 个字符",
							"c_Min": 2,
							"c_Max": 20
						}
						,
						{
							"c_Tpnm": "tViaAjax",
							"c_Info": "已被注册！",
							"c_Url": nWse.fApdUrlQry(g_SiteInfo.c_ApiUrl, "_a=is_username_available"),
							"c_Key": "username",
							"c_RstPath": "data"
						}
					]
				}
				,
				"email": {
					"Wse_Val": "",
					"Wse_Optns": {
						"c_Rqrd": 1
					},
					"Wse_Vldtrs": [
						nMvc.nVldtr.fEmail(true)
						,
						{
							"c_Tpnm": "tViaAjax",
							"c_Info": "已被注册！",
							"c_Url": nWse.fApdUrlQry(g_SiteInfo.c_ApiUrl, "_a=is_email_available"),
							"c_Key": "email",
							"c_RstPath": "data"
						}
					]
				}
				,
				"password": {
					"Wse_Val": "",
					"Wse_Optns": {
						"c_Rqrd": 1
					},
					"Wse_Vldtrs": [
						{
							"c_Tpnm": "tStrLenRge",
							"c_Info": "密码长度 8 - 20 个字符",
							"c_Min": 8,
							"c_Max": 20
						}
					]
				}
				,
				"password2": {
					"Wse_Val": "",
					"Wse_Optns": {
						"c_Rqrd": 1
					},
					"Wse_Vldtrs": [
						nMvc.nVldtr.fSpw(true)
					]
				}
			};
			l_Mdl = new nMvc.tMdl(l_MdlData, true);
			l_Mdl.cAllFldsAcpErr(true);
			l_RootMdl.cCrtFld("signup", l_Mdl);

			l_RootMdl.cCrtFld("captcha",
				{
					"Wse_Val": "",
					"Wse_Optns": {
						"c_Rqrd": 1,
					//	"c_Flag": nMvc.tMdl.tFlag.i_Hide
					}
				});
		}

		//-------- 命令
		function fInitCmds()
		{
			stAppCnsl.cReg("chg_tab", function(a_Help, a_Istr, a_Agms)
			{
				if (a_Help) { return a_Istr; }

				var l_Json = stAppCnsl.cPseAgmToJson(a_Agms, 0);
				l_RootMdl.cUpdFld("CrntTab", l_Json.c_NewName);
			});

			stAppCnsl.cReg("sbmt", function(a_Help, a_Istr, a_Agms)
			{
				if (a_Help) { return a_Istr; }

				return fSbmtForm(l_RootMdl.cReadFld("CrntTab"));
			});

		}

		//-------- 功能

		function fSbmtForm(a_Which)
		{
			var l_VM = l_RootMdl.cReadFld(a_Which);
			if (l_VM.cIsLock())
			{
				return "禁止重复提交！";
			}

			var l_VR = l_VM.cVldt();
			if (!l_VR) { return "有错误，不能提交"; }

			// 锁定模型
			l_VM.cLock();
			var l_SJ = l_VM.cToJson(null, false);
			l_SJ["captcha"] = l_RootMdl.cReadFld("captcha");

			var l_Ajax = new nWse.tAjax();
			l_Ajax.cSetRqstHdr_FormUrlEcd();
			l_Ajax.cRqst({
				c_Mthd: "POST",
				c_Url: nWse.fApdUrlQry(g_SiteInfo.c_ApiUrl, "_a=" + a_Which),
				c_Body: 'json=' + JSON.stringify(l_SJ),
				c_fOnCplt: function ()
				{
					l_VM.cUlok();

					// if (l_SbmtLA)
					// {
					// 	l_SbmtLA.cStop();
					// }
				},
				c_fOnSucs: function ()
				{
					// if (l_WgtForm)
					// {
					// 	l_WgtForm.vcShowReport(+1);
					// }
					location.href = g_SiteInfo.c_RootUrl;
				},
				c_fOnFail: function ()
				{
					// if (l_WgtForm)
					// {
					// 	l_WgtForm.vcShowReport(-1);
					// }

					var l_RspsJson = l_Ajax.cGetRspsJson();
					alert(l_RspsJson["err_msg"]);
				}
			});
		}
	});
})();